package models

import (
	orm "go-papers/database"
	"go-papers/tools"
	_ "time"
)

type MemberMomLevel struct {
	Id         int    `json:"id" gorm:"type:int(11) unsigned;primary_key"` //
	MomUserId  int    `json:"momUserId" gorm:"type:int(11);"`              // 社区达人
	PupilId    int    `json:"pupilId" gorm:"type:int(11);"`                // 徒弟
	PupilLevel int    `json:"pupilLevel" gorm:"type:int(11);"`             // 徒弟 当前等级
	MomLevel   int    `json:"momLevel" gorm:"type:int(4);"`                // 当前升级后等级
	DataScope  string `json:"dataScope" gorm:"-"`
	Params     string `json:"params"  gorm:"-"`
	BaseModel
}

func (MemberMomLevel) TableName() string {
	return "member_mom_level"
}

// 获取MemberMomLevel
func (e *MemberMomLevel) GetById(mid int, pid int, nowLevel int) (MemberMomLevel, error) {
	var doc MemberMomLevel
	table := orm.Eloquent.Table(e.TableName())
	table.Where("mom_user_id = ?", mid)
	table.Where("pupil_id = ?", mid)
	table.Where("pupil_level = ?", nowLevel)

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

// 创建MemberMomLevel
func (e *MemberMomLevel) Create() (MemberMomLevel, error) {
	var doc MemberMomLevel
	result := orm.Eloquent.Table(e.TableName()).Create(&e)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *e
	return doc, nil
}

// 获取MemberMomLevel
func (e *MemberMomLevel) Get() (MemberMomLevel, error) {
	var doc MemberMomLevel
	table := orm.Eloquent.Table(e.TableName())

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

// 获取MemberMomLevel带分页
func (e *MemberMomLevel) GetPage(pageSize int, pageIndex int) ([]MemberMomLevel, int, error) {
	var doc []MemberMomLevel

	table := orm.Eloquent.Select("*").Table(e.TableName())

	// 数据权限控制(如果不需要数据权限请将此处去掉)
	dataPermission := new(DataPermission)
	dataPermission.UserId, _ = tools.StringToInt(e.DataScope)
	table, err := dataPermission.GetDataScope(e.TableName(), table)
	if err != nil {
		return nil, 0, err
	}
	var count int

	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`deleted_at` IS NULL").Count(&count)
	return doc, count, nil
}

// 更新MemberMomLevel
func (e *MemberMomLevel) Update(id int) (update MemberMomLevel, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id = ?", id).First(&update).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}
	return
}

// 删除MemberMomLevel
func (e *MemberMomLevel) Delete(id int) (success bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id = ?", id).Delete(&MemberMomLevel{}).Error; err != nil {
		success = false
		return
	}
	success = true
	return
}

//批量删除
func (e *MemberMomLevel) BatchDelete(id []int) (Result bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id in (?)", id).Delete(&MemberMomLevel{}).Error; err != nil {
		return
	}
	Result = true
	return
}
